#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2024/9/6 11:11
# @File           : feature
# @IDE            : PyCharm
# @desc           : 描述
from datetime import datetime
from typing import List

from sqlalchemy.orm import Mapped, mapped_column, relationship
from db.db_base import AuditModel, BaseModel
from sqlalchemy import String, DateTime, ForeignKey, Integer, Text, Boolean


class DCOrder(AuditModel):
    __tablename__ = "dc_check_order"
    __table_args__ = ({'comment': '委托试验单'})

    order_type: Mapped[str] = mapped_column(String(20), comment="试验单类型，例如 PCLR, NDT")
    order_no: Mapped[str] = mapped_column(String(20), unique=True, comment="试验编号")

    qar_no: Mapped[str] = mapped_column(String(20), comment="QAR编号")
    product_type: Mapped[str] = mapped_column(String(20), comment="产品型号(机型)")

    part_type: Mapped[str] = mapped_column(String(20), comment="材料类型")
    form_type: Mapped[str] = mapped_column(String(100), comment="表单类型，例如 SAMC 05-3(2012-06)")

    status: Mapped[str] = mapped_column(String(10), comment="表单状态，包括实验中、归档等")

    prepared_by: Mapped[str] = mapped_column(String(50), nullable=True, comment="编制者")
    prepared_date: Mapped[datetime] = mapped_column(DateTime, nullable=True, comment="试验单创建日期")
    prepared_dept: Mapped[str] = mapped_column(String(30), nullable=True, comment="部门")
    prepared_phone: Mapped[str] = mapped_column(String(20), nullable=True, comment="手机号")

    finished_date: Mapped[datetime] = mapped_column(DateTime, nullable=True, comment="试验完成日期")

    material_description: Mapped[str] = mapped_column(Text, nullable=True, comment="材料说明")

    vendor: Mapped[str] = mapped_column(String(40), nullable=True, comment="卖主")
    po: Mapped[str] = mapped_column(String(30), nullable=True, comment="订单")

    test_document: Mapped[str] = mapped_column(String(100), nullable=True, comment="实验文件")
    lot_size: Mapped[str] = mapped_column(String(30), nullable=True, comment="批量")

    approved_by: Mapped[str] = mapped_column(String(50), nullable=True, comment="审核人员，工号、姓名、时间")
    
    meet_spec_requirements: Mapped[str] = mapped_column(String(30), nullable=True, comment="符合规范要求")
    
    received_by: Mapped[str] = mapped_column(String(50), nullable=True, comment="收料人，PCLR实验时带出表单检验人员")
    feeded_by: Mapped[str] = mapped_column(String(50), nullable=True, comment="送料人，PCLR实验人员刷卡，工号、姓名、时间")

    test_type: Mapped[str] = mapped_column(String(30), nullable=True, comment="实验分类，PCLR会有多个分类，如机性实验、金相实验")
    is_urgent: Mapped[bool] = mapped_column(Boolean, default=False, comment="是否加急")
    is_heavy_urgent: Mapped[bool] = mapped_column(Boolean, default=False, comment="是否特急")

    require_finish_datetime: Mapped[datetime] = mapped_column(DateTime, nullable=True, comment="需要完成时间")

    progresses: Mapped[List["DCOderProgress"]] = relationship(back_populates='order')
    contents: Mapped[List["DCOrderContent"]] = relationship(back_populates='order')


class DCOderProgress(AuditModel):
    __tablename__ = "dc_check_order_progress"
    __table_args__ = ({'comment': '委托试验单进度'})

    order_id: Mapped[int] = mapped_column(
        Integer,
        ForeignKey("dc_check_order.id", ondelete='CASCADE'),
        comment="委托试验单ID"
    )
    order: Mapped[DCOrder] = relationship(foreign_keys=order_id, back_populates='progresses')

    content: Mapped[str] = mapped_column(String(20), comment="进行说明内容")
    status: Mapped[str] = mapped_column(String(40), comment="添加进度时表单状态")


class DCOrderContent(AuditModel):
    __tablename__ = "dc_check_order_content"
    __table_args__ = ({'comment': '委托试验单内容'})

    order_id: Mapped[int] = mapped_column(
        Integer,
        ForeignKey("dc_check_order.id", ondelete='CASCADE'),
        comment="委托试验单ID"
    )
    order: Mapped[DCOrder] = relationship(foreign_keys=order_id, back_populates='contents')

    test_name: Mapped[str] = mapped_column(String(20), comment="实验名称")
    attachment: Mapped[str] = mapped_column(Text, nullable=True, comment="附件")

    is_pass: Mapped[bool] = mapped_column(Boolean, default=True, comment="是否符合规范要求")

    test_performed_by: Mapped[str] = mapped_column(String(50), nullable=True, comment="实验员")
    operation: Mapped[str] = mapped_column(Text, nullable=True, comment="操作")
